package cn.com.lyj6851.zuul.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.lyj6851.zuul.service.FeignClientApiService;

@RestController
public class MicroZuulGatewayController {
	@Autowired
    OAuth2RestTemplate oAuth2RestTemplate;
	@Autowired
	FeignClientApiService feignClientApiService;
	
	
    @GetMapping("/authuser")
    public Authentication user(Authentication user) {
        return user;
    }
    
    @GetMapping("resource")
    public String getres() {
    	return "网关开放资源";
    }
    
    @GetMapping("private")
    public String getpriv() {
    	return "网关授权资源";
    }
    
    @GetMapping("fen")
    public String fenstr() {
    	return feignClientApiService.getstr();
    }
    
    @RequestMapping("/api")
    String home(Model model) {
        String result = oAuth2RestTemplate.getForObject("http://localhost:8085/resource/api" + "/2", String.class);
        return result;
    }

    @Bean
    OAuth2RestTemplate oAuth2RestTemplate(OAuth2ClientContext oAuth2ClientContext, OAuth2ProtectedResourceDetails details){
        return new OAuth2RestTemplate(details,oAuth2ClientContext);
    }
}
